package com.wf.ew.quartz;

import com.wf.ew.common.constant.UserConstant;
import com.wf.ew.orderB.model.Bill;
import com.wf.ew.orderB.model.Car;
import com.wf.ew.orderB.model.Customer;
import com.wf.ew.orderB.service.BillService;
import com.wf.ew.orderB.service.CarService;
import com.wf.ew.orderB.service.CustomerService;
import com.wf.ew.system.model.User;
import com.wf.ew.webService.JKXHttp;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author : Doman
 * @version ：1.0
 * @date : 2020-02-07 14:04
 */
public class JKXScheduledJob implements Job {

    @Autowired
    public BillService billService;
    @Autowired
    public CustomerService customerService;
    @Autowired
    public CarService carService;

    private String name;

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
        List<Bill> list = JKXHttp.getBillList();
        for (Bill b: list) {
            User u = new User();
            u.setCompanyId("4");
            u.setId("1");
            Bill b2 = new Bill();
            b2.setBusiId(b.getBusiId());
            List<Bill> list2 = billService.allApiList(b2);
            if(list2 != null && list2.size() == 1) {
                b.setId(list2.get(0).getId());
                billService.update2(b, u);
            } else {
                billService.add(b, u);
                if (!b.getCustomer().equals("")) {
                    Customer customer = new Customer(b.getCustomer());
                    try {
                        customerService.add(customer, u);
                    } catch (Exception e) {
                    }
                }
                if (!b.getCarNo().equals("")) {
                    Car car = new Car();
                    car.setCarNo(b.getCarNo());
                    car.setDriverPhone(b.getDriverPhone());
                    car.setCompanyId(UserConstant.ADMIN_ID);
                    try {
                        carService.add(car, u);
                    } catch (Exception e) {
                    }
                }

            }
        }
        System.out.println("CRON ----> schedule DiaryScheduledJob is running ... + " + name + "  ---->  " + dateFormat.format(new Date()));
    }

}